package com.ecoolex.cms.system.core.service;

import java.util.ArrayList;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogContent;
import com.aliyun.openservices.log.common.LogItem;
import com.ecoolex.framework.common.exception.RunningException;
import com.google.common.collect.Lists;


@Component
public class LogService {

	@Value("${aliyun.log.endpoint}")
	private String endpoint;

	@Value("${aliyun.log.access.id}")
	private String accessId;

	@Value("${aliyun.log.access.secret}")
	private String accessKey;

	@Value("${aliyun.log.sunrisedutyfree.project}")
	private String project;

	@Value("${aliyun.log.sunrisedutyfree.logStore}")
	private String logStore;

	private Client client;

	@Autowired
	private void initClient() {
		client = new Client(endpoint, accessId, accessKey);
	}

	public void sunrisedutyfreeCouponLog(String url, Map<String, String> messageList) {

		ArrayList<LogContent> contents = new ArrayList<>();

		messageList.entrySet().forEach(message -> {
			contents.add(new LogContent(message.getKey(), message.getValue()));
		});

		LogItem item = new LogItem();
		item.SetLogContents(contents);
		RunningException.wrap(() -> client.PutLogs(project, logStore, url, Lists.newArrayList(item), null));
	}
}
